package com.iqismart.shorturl.controller.api.config;

import com.iqismart.shorturl.config.UidHolder;
import com.iqismart.shorturl.config.WebSite;
import com.iqismart.shorturl.exception.BaseException;
import com.iqismart.shorturl.model.EntityConfig;
import com.iqismart.shorturl.repository.ConfigRepository;
import com.iqismart.shorturl.repository.UserRepository;
import com.iqismart.shorturl.vo.ConfigRequest;
import com.iqismart.shorturl.vo.base.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Auther sty
 * @createTime 2020-03-05 17:02
 */
@RestController
public class ConfigController {
    @Autowired
    private ConfigRepository configRepository;

    @Autowired
    private WebSite webSite;

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/ui/config.js")
    @ResponseBody
    public String index(){
        /**
         * var config = {
         *     api: 'http://localhost:8080'
         *     ,ws: 'ws://localhost:8080/websocket'
         *     ,first: 'short'
         *     ,end: 'url'
         * }
         */
        return "var config = {\n" +
                "    api: 'http://"+ webSite.url +"'\n" +
                "    ,ws: 'ws://"+webSite.url+"/websocket'\n" +
                "    ,first: '"+webSite.first+"'\n" +
                "    ,end: '"+webSite.end+"'\n" +
                "}\n";
    }

    @RequestMapping("config/get")
    public Result get(){
        if(!userRepository.findById(UidHolder.getUid()).get().getUsername().equals("admin")){
            BaseException.failAndThrow("您不是管理员");
        }
        Map<String,String> map = new HashMap<>();
        map.put("domain",webSite.url);
        map.put("email","364337403@qq.com");
        map.put("icp","豫ICP备18000045号 ");
        for (EntityConfig entityConfig : configRepository.findAll()) {
            map.put(entityConfig.getName(),entityConfig.getValue());
        }

        return new Result(map);
    }

    @RequestMapping("config/set")
    public Result set(@RequestBody ConfigRequest request){
        if(!userRepository.findById(UidHolder.getUid()).get().getUsername().equals("admin")){
            BaseException.failAndThrow("您不是管理员");
        }


        Map<String,String> map = new HashMap<>();
        for (EntityConfig entityConfig : configRepository.findAll()) {
            map.put(entityConfig.getName(),entityConfig.getValue());
        }

        EntityConfig entityDomain = configRepository.findByName("domain");
        EntityConfig entityEmail = configRepository.findByName("email");
        EntityConfig entityIcp = configRepository.findByName("icp");
        if(entityDomain == null) entityDomain = new EntityConfig("domain");
        if(entityEmail == null) entityEmail = new EntityConfig("email");
        if(entityIcp == null) entityIcp = new EntityConfig("icp");
        entityDomain.setValue(request.getDomain());
        entityEmail.setValue(request.getEmail());
        entityIcp.setValue(request.getIcp());

        configRepository.save(entityDomain);
        configRepository.save(entityEmail);
        configRepository.save(entityIcp);



        return new Result(map);
    }
}
